/*
 * Laboration2View.java
 */

package laboration2;

import java.util.ArrayList;
import java.util.List;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.JDialog;
import javax.swing.JFrame;
import se.chalmers.ait.dat215.lab2.*;
/**
 * The application's main frame.
 */
public class Laboration2View extends FrameView {

    public Laboration2View(SingleFrameApplication app) {
        super(app);
        parent = app;

        initComponents();
        
        initLists();
        
        // Fking GUI editor.
        ((java.awt.BorderLayout)mainPanel.getLayout()).setVgap(5);
        ((java.awt.GridLayout)northPanel.getLayout()).setHgap(3);
    }
    
    private void initLists() {
        String[] cuisineItems = {"Alla", "Sverige", "Grekland", "Indien",
            "Asien", "Afrika", "Frankrike"};
        cuisine.setListData(cuisineItems);
        
        String[] mainIngrItems = {"Alla", "Kött", "Fisk", "Kyckling", "Vegetarisk"};
        mainIngredient.setListData(mainIngrItems);
        
        String[] difficultyItems = {"Alla", "Lätt", "Mellan", "Svår"};
        difficulty.setListData(difficultyItems);
        
        int n = 15; // 10 -- 150
        String[] maxTimeItems = new String[n+1];
        maxTimeItems[0] = "Alla";
        for (int i = 1; i <= n; i++) {
            maxTimeItems[i] = ""+i*10;
        }
        maxTime.setListData(maxTimeItems);
        
        ArrayList maxPriceItems = new ArrayList();
        maxPriceItems.add("Alla");
        int i = 0;
        while (i < 500) {
            if      (i< 50) i += 5;
            else if (i<150) i += 10;
            else if (i<300) i += 50;
            else            i += 100;
            maxPriceItems.add(""+i);
        }
        maxPrice.setListData(maxPriceItems.toArray());
        
        cuisine.setSelectedIndex(0);
        mainIngredient.setSelectedIndex(0);
        difficulty.setSelectedIndex(0);
        maxTime.setSelectedIndex(0);
        maxPrice.setSelectedIndex(0);
    }
    
    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = Laboration2App.getApplication().getMainFrame();
            aboutBox = new Laboration2AboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        Laboration2App.getApplication().show(aboutBox);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
  //  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        centerPanel = new javax.swing.JPanel();
        northPanel = new javax.swing.JPanel();
        cuisinePanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        cuisine = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        mainIngredientPanel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        mainIngredient = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        difficultyPanel = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        difficulty = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        maxTimePanel = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        maxTime = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        maxPricePanel = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        maxPrice = new javax.swing.JList();
        jLabel5 = new javax.swing.JLabel();
        resultsPanel = new laboration2.gui.ResultsPanel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem2 = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(700, 478));
        mainPanel.setLayout(new java.awt.BorderLayout());

        centerPanel.setName("centerPanel"); // NOI18N
        centerPanel.setLayout(new java.awt.GridLayout());
        mainPanel.add(centerPanel, java.awt.BorderLayout.SOUTH);

        northPanel.setName("northPanel"); // NOI18N
        northPanel.setLayout(new java.awt.GridLayout(1, 0));

        cuisinePanel.setName("cuisinePanel"); // NOI18N
        cuisinePanel.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        cuisine.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        cuisine.setName("cuisine"); // NOI18N
        cuisine.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                selectionChanged(evt);
            }
        });
        jScrollPane1.setViewportView(cuisine);

        cuisinePanel.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(laboration2.Laboration2App.class).getContext().getResourceMap(Laboration2View.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        cuisinePanel.add(jLabel1, java.awt.BorderLayout.PAGE_START);

        northPanel.add(cuisinePanel);

        mainIngredientPanel.setName("mainIngredientPanel"); // NOI18N
        mainIngredientPanel.setLayout(new java.awt.BorderLayout());

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        mainIngredient.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        mainIngredient.setName("mainIngredient"); // NOI18N
        mainIngredient.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                selectionChanged(evt);
            }
        });
        jScrollPane2.setViewportView(mainIngredient);

        mainIngredientPanel.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        mainIngredientPanel.add(jLabel2, java.awt.BorderLayout.PAGE_START);

        northPanel.add(mainIngredientPanel);

        difficultyPanel.setName("difficultyPanel"); // NOI18N
        difficultyPanel.setLayout(new java.awt.BorderLayout());

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        difficulty.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        difficulty.setName("difficulty"); // NOI18N
        difficulty.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                selectionChanged(evt);
            }
        });
        jScrollPane3.setViewportView(difficulty);

        difficultyPanel.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        difficultyPanel.add(jLabel3, java.awt.BorderLayout.PAGE_START);

        northPanel.add(difficultyPanel);

        maxTimePanel.setName("maxTimePanel"); // NOI18N
        maxTimePanel.setLayout(new java.awt.BorderLayout());

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        maxTime.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        maxTime.setName("maxTime"); // NOI18N
        maxTime.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                selectionChanged(evt);
            }
        });
        jScrollPane4.setViewportView(maxTime);

        maxTimePanel.add(jScrollPane4, java.awt.BorderLayout.CENTER);

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N
        maxTimePanel.add(jLabel4, java.awt.BorderLayout.PAGE_START);

        northPanel.add(maxTimePanel);

        maxPricePanel.setName("maxPricePanel"); // NOI18N
        maxPricePanel.setLayout(new java.awt.BorderLayout());

        jScrollPane5.setName("jScrollPane5"); // NOI18N

        maxPrice.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        maxPrice.setName("maxPrice"); // NOI18N
        maxPrice.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                selectionChanged(evt);
            }
        });
        jScrollPane5.setViewportView(maxPrice);

        maxPricePanel.add(jScrollPane5, java.awt.BorderLayout.CENTER);

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N
        maxPricePanel.add(jLabel5, java.awt.BorderLayout.PAGE_START);

        northPanel.add(maxPricePanel);

        mainPanel.add(northPanel, java.awt.BorderLayout.PAGE_START);

        resultsPanel.setName("resultsPanel"); // NOI18N
        mainPanel.add(resultsPanel, java.awt.BorderLayout.CENTER);

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.META_MASK));
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openNewWindow(evt);
            }
        });
        fileMenu.add(jMenuItem1);

        jSeparator1.setName("jSeparator1"); // NOI18N
        fileMenu.add(jSeparator1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.META_MASK));
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeWindow(evt);
            }
        });
        fileMenu.add(jMenuItem2);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(laboration2.Laboration2App.class).getContext().getActionMap(Laboration2View.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

private void selectionChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_selectionChanged
    
    String difficultyStr = (String)difficulty.getSelectedValue();
    difficultyStr = "Alla".equals(difficultyStr) ? null : difficultyStr;
    String maxTimeStr = (String)maxTime.getSelectedValue();
    maxTimeStr = "Alla".equals(maxTimeStr) ? "0" : maxTimeStr;
    int maxTimeInt = maxTimeStr == null ? 0 : Integer.valueOf(maxTimeStr);
    String cuisineStr = (String)cuisine.getSelectedValue();
    cuisineStr = "Alla".equals(cuisineStr) ? null : cuisineStr;
    String maxPriceStr = (String)maxPrice.getSelectedValue();
    maxPriceStr = "Alla".equals(maxPriceStr) ? "0" : maxPriceStr;
    int maxPriceInt = maxPriceStr == null ? 0 : Integer.valueOf(maxPriceStr);
    String mainIngredientStr = (String)mainIngredient.getSelectedValue();
    mainIngredientStr = "Alla".equals(mainIngredientStr) ? null : mainIngredientStr;
    
    if (difficultyStr == null && maxPriceInt == 0 && cuisineStr == null && maxPriceInt == 0 && mainIngredientStr == null) {
        maxTimeInt = 100000;
    }
    
    SearchFilter filter = new SearchFilter(difficultyStr,maxTimeInt, cuisineStr, maxPriceInt, mainIngredientStr);
   
    if (filter != null) {
        List<Recipe> recipes = RecipeDatabase.getSharedInstance().search(filter);
        List<Recipe> copyRecipes = new ArrayList<Recipe>();
        
        if (recipes != null) {
            for (Recipe r : recipes) {
                if (r.getMatch() >= 25) copyRecipes.add(r); 
            }
            
            resultsPanel.setSearchResults(copyRecipes);
        }
        
    }
}//GEN-LAST:event_selectionChanged

private void openNewWindow(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openNewWindow
    parent.show(new Laboration2View(parent));
}//GEN-LAST:event_openNewWindow

private void closeWindow(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeWindow
    parent.hide(this);
}//GEN-LAST:event_closeWindow

    //System.out.println("Title: "+((JList)evt.getSource()).getSelectedValue());
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel centerPanel;
    private javax.swing.JList cuisine;
    private javax.swing.JPanel cuisinePanel;
    private javax.swing.JList difficulty;
    private javax.swing.JPanel difficultyPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JList mainIngredient;
    private javax.swing.JPanel mainIngredientPanel;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JList maxPrice;
    private javax.swing.JPanel maxPricePanel;
    private javax.swing.JList maxTime;
    private javax.swing.JPanel maxTimePanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JPanel northPanel;
    private laboration2.gui.ResultsPanel resultsPanel;
    // End of variables declaration//GEN-END:variables
    
    private JDialog aboutBox;
    private SingleFrameApplication parent;
}
